const fs = require('fs')
const path = require('path'); //解析需要遍历的文件夹
const inImagePath = path.resolve('./src/images'); // 获得images的绝对路径
const sizeOf = require('image-size');

let arrCss = [], arrHtml = [], idx = 0, len = 0;

function fileDisplay(filePath) {
    fs.existsSync('./src/area') ? '' : fs.mkdirSync('./src/area')
    fs.readdir(filePath, function (err, files) {
        if (err) {
            console.warn(err)
        } else {
            len = files.length
            file = files.sort(function () {
                return 1
            })

            files.forEach(function (filename) {
                var filedir = path.join(filePath, filename);
                fs.stat(filedir, function (eror, stats) {
                    if (eror) {
                        console.warn('获取文件stats失败');
                    } else {
                        var isFile = stats.isFile();
                        var isDir = stats.isDirectory();
                        if (isFile && /\.(jpg|png)$/.test(filename)) {
                            // console.log(filedir,"是以.jpg结尾的文件");

                            sizeOf(filedir, function (err, dimensions) {
                                var strHtml = '<div class="area ' + filename.slice(0, -4) + '"><div class="inner"><%= require("../../area/' + filename.slice(0, -4) + '.ejs")() %></div></div>\n'

                                // 新建area.ejs
                                fs.writeFileSync('./src/area/' + filename.slice(0, -4) + '.ejs', '', function (err) {
                                    err && console.log(err)
                                })

                                if (!!filename.match(/area/g)) {
                                    var strCss = '.' + filename.slice(0, -4) + '{height:' + dimensions.height + 'px;background-image:url(../images/' + filename + ')}\n'
                                } else {
                                    var strCss = '.' + filename.slice(0, -4) + '{width:' + dimensions.width + 'px;height:' + dimensions.height + 'px;background-image:url(../images/' + filename + ')}\n'
                                }
                                arrCss.push(strCss)
                                arrHtml.push(strHtml)
                                idx++

                                if (idx == len) {
                                    // 写入html
                                    fs.writeFileSync('./src/template/page/main.ejs', arrHtml.sort().toString().replace(/,/g, ''), function (err) {
                                        if (err) console.log('img to html fail', err);
                                    })

                                    //以template/components/common/_style.css作为模板在后面加入生成的css
                                    fs.copyFileSync('./src/template/common/css/_style.css', './src/css/style.css', function (err) {
                                        if (err) console.log('copy file fail')
                                    })

                                    fs.appendFileSync('./src/css/style.css', arrCss.sort().toString().replace(/,/g, ''), function (err) {
                                        if (err) console.log('img to css fail', err);
                                    })
                                }
                            });

                        }
                        if (isDir) {
                            fileDisplay(filedir);
                        }
                    }
                })
            });

        }
    });
}

fileDisplay(inImagePath);


